<?php

namespace Admin\Controller;

use Think\Controller;

class DesignTaskController extends BaseController
{
    /*
     * 获取列表
     */
    public function index()
    {
        if (IS_POST) {

            $tea = M('designtask');
            if (!isset($_POST['page']) || empty($_POST['page'])) {
                $page = 1;
            } else {
                $page = $_POST['page'];
            }
            if (!isset($_POST['limit']) || empty($_POST['limit'])) {
                $limit = 10;
            } else {
                $limit = $_POST['limit'];
            }
            $condition['is_del'] = 0;
            if (isset($_POST['search'])) {
                $condition[] = [
                    'course_id' => array('like', "%" . $_POST['search'] . "%"),
                    'course_name' => array('like', "%" . $_POST['search'] . "%"),
                    'teacher_id' => array('like', "%" . $_POST['search'] . "%"),
                    'teacher_name' => array('like', "%" . $_POST['search'] . "%"),
                    'class_name' => array('like', "%" . $_POST['search'] . "%"),
                    'term' => array('like', "%" . $_POST['search'] . "%"),
                    '_logic' => 'OR',
                ];
            }
            //添加删除字段
            $count = $tea->where($condition)->count();
            $data = $tea->where($condition)->order('id desc')->page($page, $limit)->select() ?: [];

            return $this->successResponse('获取成功', $data, $count);
        } else {
            $this->display();
        }
    }

    /*
     * 课程设计任务删除
     */
    public function del()
    {
        if (IS_POST) {
            $id = $_POST['id'];
            $model = M('designtask');
            $design_old_task = $model->where('id = ' . $id)->field('teacher_id, term')->find();
            if (!empty($design_old_task)) {

                $data = array(
                    'is_del' => 1
                );
                $rs = $model->where('id = ' . $id)->data($data)->save();
                if ($rs === false) {
                    $this->ajaxReturn(false);
                } else {
                    $this->dealWorkload($design_old_task['teacher_id'], $design_old_task['term'], 2);
                    $this->ajaxReturn(true);
                }
            } else {
                $this->error('所选信息不存在');
            }
        } else {
            $this->error('请求方式有误');
        }
    }

    /*
     * 课程设计任务添加
     */
    public function add()
    {
        $practice = M('practice');
        $teacher = M('teacher');
        $class = M('class');
        $year = M('termyear');
        //自动收集表单
        if (empty($_POST))   //首先判断时侯为第一次打开页面
        {
            $where = [
                'is_del' => 0,
            ];
            $course_count = $practice->where($where)->count('course_id');
            if ($course_count > 0) {
                $course_info = $practice->where($where)->field('course_id, course_name')->select() ?: [];
            } else {
                $course_info = [];
            }

            $teacher_count = $teacher->where($where)->count('teacher_id');
            if ($teacher_count) {
                $teacher_info = $teacher->where($where)->field('teacher_id, teacher_name')->select() ?: [];
            } else {
                $teacher_info = [];
            }

            $class_count = $class->where($where)->count('class_id');
            if ($class_count) {
                $class_info = $class->where($where)->order("class_name asc")->field('class_id, class_name')->select() ?: [];
            } else {
                $class_info = [];
            }

            //参考例子
            $year_info = $year->where($where)->order("year asc,term asc")->field('t_id, year, term')->select() ?: [];
            $data = [
                'course_info' => $course_info,
                'teacher_info' => $teacher_info,
                'class_info' => $class_info,
                'year_info' => $year_info,
            ];
            $this->assign($data);
            $this->display();
        } else {
            
            $system = M('system');
            //拿到周数
            $course_info = $practice->where('course_id =' . $_POST['course_id'])->field('course_name, weeks')->find();
            $teacher_info = $teacher->where('teacher_id = ' . $_POST['teacher_id'])->field('teacher_id, teacher_name')->find();
            $class_info = $class->where('class_id = ' . $_POST['class_id'])->field('class_name, student_number')->find();
            $term_year = $this->dealTerm($_POST['termyear_id']);
            $coe = $system->where('coe_id = 1')->field('coe_c')->find();
            $work = $this->calculation($course_info['weeks'], $class_info['student_number'], $coe['coe_c']);
            //存表
            $data = [
                'course_name' => $course_info['course_name'],
                'course_id' => $_POST['course_id'],
                'teacher_id' => $teacher_info['teacher_id'],
                'teacher_name' => $teacher_info['teacher_name'],
                'class_name' => $class_info['class_name'],
                'term' => $term_year,
                'practice_workload' => $work,
                'note' => $_POST['note'],
                'is_del' => 0,
            ];
            $DesignTask = M('designtask');
            $DesignTask->create();
            $res = $DesignTask->add($data);
            if ($res) {
                $this->dealWorkload($teacher_info['teacher_id'], $term_year, 2);
                $this->success("添加成功,返回页面", U("index"), 1);
            } else {
                $this->error("添加失败！", U("index"), 1);
            }
        }
    }

    /*
     * 处理学年学期
     */
    protected function dealTerm($termyear_id)
    {
        $termyear = M('termyear');
        $termyear_info = $termyear->where('t_id = ' . $termyear_id)->field('year, term')->find();
        $next_year = $termyear_info['year'] + 1;
        $term = $termyear_info['term'] == 1 ? '一' : '二';
        $term_year = $termyear_info['year'] . '-' . $next_year . '学年第' . $term . '学期';
        return $term_year;
    }

    /*
     * 计算A2值
     */
    protected function calculation($course_weeks, $class_people_num, $coe_c = 80)
    {
        if ($coe_c == 0) {
            $this->error('A2基数设置不能为0');
        }
        //计算A2值
        $A2 = 0;
        $num = round(($class_people_num / $coe_c), 2);
        if ($num <= 1) {    //小于或等于80
            $A2 = $class_people_num * 0.05;
        } elseif ($num > 1) {
            $A2 = 4;
        } else {
            $this->error('系统错误，请联系技术人员');
        }
        $work = $course_weeks * $A2 * 10;
        return $work;
    }

    /*
     * 信息更新
     */
    public function update()
    {

        if (empty($_POST)) {
            $this->error('缺少输入值');
        }
        /*
          'course_id' => string '18521' (length=5)
          'teacher_id' => string '123144' (length=6)
          'class_id' => string '17581' (length=5)
          'term_id' => string '6' (length=1)
          'note' => string '' (length=0)
         */
        $practice = M('practice');
        $teacher = M('teacher');
        $class = M('class');
        $system = M('system');
        //拿到周数
        $course_info = $practice->where('course_id =' . $_POST['course_id'])->field('course_name, weeks')->find();
        $teacher_info = $teacher->where('teacher_id = ' . $_POST['teacher_id'])->field('teacher_id, teacher_name')->find();
        $class_info = $class->where('class_id = ' . $_POST['class_id'])->field('class_name, student_number')->find();
        $term_year = $this->dealTerm($_POST['term_id']);
        $coe = $system->where('coe_id = 1')->field('coe_c')->find();
        $work = $this->calculation($course_info['weeks'], $class_info['student_number'], $coe['coe_c']);
        //存表
        $data = [
            'course_name' => $course_info['course_name'],
            'course_id' => $_POST['course_id'],
            'teacher_id' => $teacher_info['teacher_id'],
            'teacher_name' => $teacher_info['teacher_name'],
            'class_name' => $class_info['class_name'],
            'term' => $term_year,
            'practice_workload' => $work,
            'note' => $_POST['note'],
            'is_del' => 0,
        ];
        $DesignTask = M('designtask');
        $design_old_task = $DesignTask->where('id =' . $_POST['design_task_id'])->field('teacher_id, term')->find();
        $res = $DesignTask->where('id =' . $_POST['design_task_id'])->save($data);
        if ($res === false) {
            $this->error("修改失败！", U("index"), 1);
        }
        $this->dealWorkload($teacher_info['teacher_id'], $term_year, 2);
        if ($design_old_task['teacher_id'] != $teacher_info['teacher_id'] || $design_old_task['term'] != $term_year) {
            $this->dealWorkload($design_old_task['teacher_id'], $design_old_task['term'], 2);
        }
        $this->success("修改成功,返回页面", U("index"), 1);

    }

    /**
     * 获取教师详情页
     * @author llj <1063944289@qq.com>
     */
    public function designtaskDetail()
    {
        if ($_POST['id']) {
            $designtask = M('designtask');
            $practice = D("practice");
            $teacher = D("teacher");
            $class = D("class");
            $year = D("termyear");
            $where = [
                'is_del' => 0,
            ];
            $course_info = $practice->where($where)->field('course_id, course_name')->select() ?: [];

            $teacher_info = $teacher->where($where)->field('teacher_id, teacher_name')->select() ?: [];

            $class_info = $class->where($where)->field('class_id, class_name')->select() ?: [];

            $year_info = $year->where($where)->field('t_id, year, term')->select() ?: [];

            $col = [
                'course_info' => $course_info,
                'teacher_info' => $teacher_info,
                'class_info' => $class_info,
                'year_info' => $year_info,
            ];
            $this->assign($col);
            $data = $designtask->find($_POST['id']);
            if ($data) {
                $design_task['design_task_id'] = $data['id'];
                $this->assign($design_task);
                $this->assign($data);
                $this->display('designtaskDetail');
            } else {
                $this->error('id不存在');
            }
        } else {
            $this->error('参数有误');
        }
    }


}

